package com.webflux.handlers;

import com.webflux.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class Service1Handler {

    @Autowired
    private WebClient.Builder builder;

    public Mono<ServerResponse> findProduct(ServerRequest request){

        Integer productId = Integer.valueOf(request.pathVariable("productId"));
        System.out.println("产品ID"+productId);
        return builder.build()
                .get().uri("/s2/find/{productId}",productId)
                .retrieve().bodyToMono(Product.class)
                .flatMap(p ->ServerResponse.ok().bodyValue(p));

    }
}
